package com.pengtu.web.detect;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.entity.app.Attachment;
import com.pengtu.entity.detect.grade.Projquacheck;
import com.pengtu.entity.detect.grade.Subentry;
import com.pengtu.service.detect.ProjquacheckManager;
import com.pengtu.utils.ExcelHelper;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/detect/grade")
@Result(name = BaseActionSupport.RELOAD, location = "/detect/grade/projquacheck.action", type = "redirect")
public class ProjquacheckAction extends BaseActionSupport<Projquacheck> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ProjquacheckManager projquacheckManager;
	private String id;
	private Projquacheck projquacheck;
	private List<Projquacheck> projquacheckList = new ArrayList<Projquacheck>();
	private String subCodeString = "";
	private Attachment attachment;
	private String checkProjIds;

	private String[] entryIdArr = {};

	public String[] getEntryIdArr() {
		return entryIdArr;
	}

	public void setEntryIdArr(String[] entryIdArr) {
		this.entryIdArr = entryIdArr;
	}

	@Override
	public Projquacheck getModel() {
		return projquacheck;
	}

	@Override
	public String list() throws Exception {
		projquacheckList = projquacheckManager.findAllProjquacheck();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// 获得抽查项目
		// checkProjIds = Struts2Utils.getParameter("checkProjIds");
		// // 构造单位工程集合
		// List<UnitProj> unitProjList = new ArrayList<UnitProj>();
		// // 构造分部集合
		// List<Branch> branchList = new ArrayList<Branch>();
		// // 构造分项集合
		// List<Subentry> subentryList = new ArrayList<Subentry>();
		// // 声明抽查项目的集合
		// List<Projquacheck> checkProjList = new ArrayList<Projquacheck>();
		//
		// for (int i = 0; i < entryIdArr.length; i++) {
		// if (StringUtils.isNotEmpty(entryIdArr[i])) {
		// Projquacheck projqua =
		// projquacheckManager.findByProperty("id",entryIdArr[i]);
		// checkProjList.add(projqua);
		// }
		// }
		// for (int i = 0; i < checkProjList.size(); i++) {
		//
		// String unitProjPoint = "";
		// String branchPoint = "";
		// if (i != 0) {
		// unitProjPoint = checkProjList.get(i - 1).getUnitProjCode();
		// branchPoint = checkProjList.get(i-1).getBranchProjCode();
		// }
		// if (!checkProjList.get(i).getUnitProjCode().equals(unitProjPoint)) {
		//
		// UnitProj unitProj = new UnitProj();
		// unitProj.setUnitProjName(checkProjList.get(i).getUnitProj());
		// unitProj.setUnitProjCode(checkProjList.get(i).getUnitProjCode());
		// unitProjList.add(unitProj);
		// }
		//
		// if (!checkProjList.get(i).getBranchProjCode().equals(branchPoint)) {
		//
		// Branch branch = new Branch();
		// branch.setBranchName(checkProjList.get(i).getBranchProjType());
		// branch.setBranchCode(checkProjList.get(i).getBranchProjCode());
		// branch.setBranchValue(checkProjList.get(i).getBranchProjValue());
		// branchList.add(branch);
		// }
		//
		//
		// Subentry subentry = new Subentry();
		// subentry.setSubentryName(checkProjList.get(i).getCheckProjName());
		// subentry.setSubentryCode(checkProjList.get(i).getCheckProjCode());
		// subentry.setSubentryValue(checkProjList.get(i).getCheckProjValue());
		// subentryList.add(subentry);
		//
		// }
		//
		// for (int i = 0; i < unitProjList.size(); i++) {
		// String unitProjCode = unitProjList.get(i).getUnitProjCode();
		// List<Branch> temporaryBranchList = new ArrayList<Branch>();
		// for (int j = 0; j < branchList.size(); j++) {
		// String branchCode = branchList.get(j).getBranchCode();
		// List<Subentry> temporarySubentryList = new ArrayList<Subentry>();
		// if(branchList.get(j).getBranchCode().substring(0,
		// 3).equals(unitProjCode)){
		// temporaryBranchList.add(branchList.get(j));
		// }
		// for (int k = 0; k < subentryList.size(); k++) {
		// if(subentryList.get(k).getSubentryCode().substring(0,
		// 5).equals(branchCode)){
		// temporarySubentryList.add(subentryList.get(k));
		// }
		// }
		// branchList.get(j).setSubentryList(temporarySubentryList);
		// }
		// unitProjList.get(i).setBranchList(temporaryBranchList);
		// }
		//
		// Struts2Utils.getRequest().setAttribute("unitProjList", unitProjList);
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		projquacheckManager.saveProjquacheck(projquacheck);
		return RELOAD;
	}

	public String finalSave() {

		return "success";
	}

	public String findAllProjQuaCheck() {
		String signcheckId = Struts2Utils.getParameter("singcheckId");
		if (StringUtils.isNotEmpty(signcheckId)) {
			List<Subentry> subList = projquacheckManager
					.searchSubList(signcheckId);
			if (null != subList) {
				if (subList.size() > 0) {
					for (int i = 0; i < subList.size(); i++) {
						if(i!=0){
							subCodeString +=",";
						}
						subCodeString += subList.get(i).getSubentryCode();
					}
				}
			}
		}
		projquacheckList = projquacheckManager.findAllProjquacheck();

		return "batch";
	}

	/**
	 * 
	 * importassets: 适用:从Excel导入数据保存
	 * 
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	public String importassets() {

		if (null != attachment) {
			try {
				File file = attachment.getUpfile();
				Workbook book = Workbook.getWorkbook(file);
				// 获得第一个工作表对象
				Sheet sheet = book.getSheet(0);
				int rownum = sheet.getRows(); // 得到行数
				// 得到第一列第一行的单元格
				ExcelHelper excelHelper = ExcelHelper.getInstance();
				for (int i = 1; i < rownum; i++) // 循环进行读写
				{
					if (StringUtils
							.isEmpty(excelHelper.getValueAt(sheet, i, 0)))
						continue;

					Projquacheck projquacheck = new Projquacheck();

					projquacheck.setUnitProj(excelHelper
							.getValueAt(sheet, i, 0));
					projquacheck.setUnitProjCode(excelHelper.getValueAt(sheet,
							i, 1));

					projquacheck.setBranchProjType(excelHelper.getValueAt(
							sheet, i, 2));
					projquacheck.setBranchProjValue(excelHelper.getValueAt(
							sheet, i, 3));
					projquacheck.setBranchProjCode(excelHelper.getValueAt(
							sheet, i, 4));

					projquacheck.setCheckProjName(excelHelper.getValueAt(sheet,
							i, 5));
					projquacheck.setCheckProjCode(excelHelper.getValueAt(sheet,
							i, 6));
					projquacheck.setCheckProjValue(excelHelper.getValueAt(
							sheet, i, 7));

					projquacheck.setRemark(excelHelper.getValueAt(sheet, i, 8));
					projquacheckManager.saveProjquacheck(projquacheck);
				}
				book.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			return "successMessage";
		}
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			projquacheck = projquacheckManager.findByProperty("id", id);
		} else {
			projquacheck = new Projquacheck();
		}
	}

	@Autowired
	public void setProjquacheckManager(ProjquacheckManager projquacheckManager) {
		this.projquacheckManager = projquacheckManager;
	}

	public List<Projquacheck> getProjquacheckList() {
		return projquacheckList;
	}

	public void setProjquacheckList(List<Projquacheck> projquacheckList) {
		this.projquacheckList = projquacheckList;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	public Attachment getAttachment() {
		return attachment;
	}

	public String getCheckProjIds() {
		return checkProjIds;
	}

	public void setCheckProjIds(String checkProjIds) {
		this.checkProjIds = checkProjIds;
	}

	public String getSubCodeString() {
		return subCodeString;
	}

}
